# coding: utf-8
# @Author: cyl
# @File: 05requests_handle.py
# @Time: 2024/08/19 20:22:41
import httpx
import requests
import ujson
import warnings
from requests import Response

requests.packages.urllib3.disable_warnings()
warnings.filterwarnings('ignore')

# requests.get()
# requests.request("get", ...)
# delete: xxx/xxx?id=123456

"""
url: prefix(http://www.baidu.com) + uri(params, eg: ?a=1&b=2&c=4)

data json file: 一样的, 区别在于后端怎么定义? 
    content-type="application/json" -> json
    content-type="wwwenoce..." -> data 
    content-type="file..." -> file

allow_redirects: 重定向 200 前端直接请求的接口 30x 后端做的重定向(调用接口a, 实际走的接口是b)

proxies: 代理 爬虫比较多

headers: dict = {
    "access-token": "xxx"
}
session.request(headers=headers)
session.request("get", url="xxx")


requestBody、responseBody
"""

res: Response = requests.get("https://www.baidu.com")
print(res)
print(res.history)
print(res.url)
print(str(res.content, encoding="utf-8"))


# noinspection DuplicatedCode
# class RequestHandle:
#     """requests请求封装"""

#     @staticmethod
#     def required(method, url, data=None, headers=None) -> Response:
#         """application为表单格式"""
#         res = ""
#         if method == "get":
#             res = requests.get(url=url, params=data, headers=headers, verify=False)
#         if method == "post":
#             res = requests.post(url=url, data=data, headers=headers, verify=False)
#         if method == "put":
#             res = requests.put(url=url, data=data, headers=headers, verify=False)
#         if method == "delete":
#             res = requests.delete(url=url, params=data, headers=headers, verify=False)
#         if method == "patch":
#             res = requests.patch(url=url, data=data, headers=headers, verify=False)
#         return res

#     @staticmethod
#     def required_json(method, url, data=None, json=None, headers=None) -> Response:
#         """application为json格式"""
#         res = ""
#         if method == "get":
#             res = requests.get(url=url, params=data, verify=False)
#         if method == "post":
#             res = requests.post(url=url, json=json, headers=headers, verify=False)
#         if method == "put":
#             res = requests.put(url=url, data=data, headers=headers, verify=False)
#         if method == "delete":
#             res = requests.delete(url=url, params=data, headers=headers, verify=False)
#         if method == "patch":
#             res = requests.patch(url=url, json=json, headers=headers, verify=False)
#         return res

#     @staticmethod
#     def visited(method: str, url: str, **kwargs) -> Response:
#         return requests.request(method=method.upper(), url=url, verify=False, **kwargs)

#     def currency_request(
#             self,
#             _module: str,
#             method: str,
#             point: str,
#             content_type: str = "application/json;charset=UTF-8",
#             is_bim: bool = True,
#             resful: bool = True,
#             **kwargs
#     ) -> dict | Response:
#         """
#         通用请求
#         :param _module: request module
#             客户端质检cas、xml(区分json和xml格式): cas_request
#             客户端ccid: cas_ccid
#             客户端ccid_data: case_ccid_data
#             工程数字平台: egp_platform
#             试验室: laboratory
#             web计量: metering
#             工程一体化: board
#             工程一体化: bff
#             center: center
#         :param method: request method
#         :param point: request uri
#         :param content_type: headers type
#         :param is_bim: 接口是否以/LBIM开头
#         :param resful: 是否序列化输出
#         """
#         if method not in self.methods:
#             raise Exception("request method is not allowed!")
#         headers = {
#             "Content-Type": content_type,
#             "Connection": "Keep-Alive",
#             "Accept-Encoding": "gzip,deflate"
#         }
#         match _module:
#             case "cas_request":
#                 # content_type = "text/xml;charset=utf-8"
#                 headers["access-token"] = self.db.get("ClientAccessToken")
#                 if self.db.get("cookies"):
#                     headers["Cookie"] = self.db.get("cookies")
#                 _url: str = app_cof.CLIENT + point if is_bim else app_cof.PDS + point
#             case "cas_ccid":
#                 headers["sign"] = self.db.get("ccid_token")
#                 _url: str = self.db.get("ccid_context") + point
#             case "cas_ccid_data":
#                 headers["sign"] = self.db.get("ccid_data_token")
#                 _url: str = self.db.get("ccid_data_context") + point
#             case "egp_platform":
#                 headers["access-token"] = self.db.get("engineeringDigitalPlatformToken")
#                 _url: str = app_cof.LaboratoryWebUIL + point
#             case "laboratory":
#                 headers["access-token"] = self.db.get("LaboratoryToken")
#                 _url: str = app_cof.LaboratoryWebUIL + point
#             case "metering":
#                 headers["access-token"] = self.db.get("WebMeteringToken")
#                 _url: str = app_cof.WebMeteringWebUIL + point
#             case "pro_board":
#                 headers["access-token"] = self.db.get("boardHeader")
#                 _url: str = app_cof.IntegratedBoardClient + point
#             case "pro_bff":
#                 headers["access-token"] = self.db.get("boardHeader")
#                 headers["authorization"] = "Basic bGlsZUA5MDAwMTAwMDoxMTExMTE="
#                 _url: str = app_cof.IntegratedWebURL + point
#             case "center":
#                 headers["access-token"] = self.db.get("CenterToken")
#                 _url: str = app_cof.PDS + point
#             case _:
#                 raise Exception("request module to be implemented!")
#         params: dict = kwargs.get("params", {})
#         logger.info("{}, {}".format(method, point + dictToUriStr(params)))

#         res = self.visited(method, _url, headers=headers, **kwargs)
#         assert res.status_code in (200, 204, 500, 400)
#         if resful:
#             return ujson.loads(str(res.content, encoding="utf-8"))
#         return res

#     # 1. 客户端质检
#     def cas_request(
#             self,
#             method: str,
#             point: str,
#             isLBim: bool = True,
#             resJson: bool = False,
#             **kwargs
#     ) -> Any | Response:
#         """cas客户端"""
#         return self.currency_request(
#             _module="cas_request",
#             method=method,
#             point=point,
#             is_bim=isLBim,
#             resful=resJson,
#             **kwargs
#         )

#     def cas_request_xml(
#             self,
#             method: str,
#             point: str,
#             content_type="text/xml;charset=utf-8",
#             isLBim: bool = True,
#             **kwargs
#     ) -> Any | Response:
#         """cas xml"""
#         return self.currency_request(
#             _module="cas_request",
#             method=method,
#             point=point,
#             content_type=content_type,
#             is_bim=isLBim,
#             resful=False,
#             **kwargs
#         )

#     def ccid_request(self, method: str, point: str, resful: bool = False, **kwargs) -> Any | Response:
#         """ccid"""
#         return self.currency_request(
#             _module="cas_ccid",
#             method=method,
#             point=point,
#             resful=resful,
#             **kwargs
#         )

#     def ccid_data_request(self, method: str, point: str, **kwargs):
#         """ccid_data"""
#         return self.currency_request(
#             _module="cas_ccid_data",
#             method=method,
#             point=point,
#             resful=False,
#             **kwargs
#         )

#     # 2. 工程数字平台、试验室
#     def egp_platform(
#             self,
#             method: str,
#             point: str,
#             resful=True,
#             **kwargs
#     ) -> T | Response:
#         """工程数字平台"""
#         res: T = self.currency_request(
#             _module="egp_platform",
#             method=method,
#             point=point,
#             resful=resful,
#             **kwargs
#         )
#         return res

#     def required_laboratory(
#             self,
#             method: str,
#             point: str,
#             resful=True,
#             **kwargs
#     ) -> T | Response:
#         """试验室"""
#         res: T = self.currency_request(
#             _module="laboratory",
#             method=method,
#             point=point,
#             resful=resful,
#             **kwargs
#         )
#         return res

#     # 3. 工程一体化
#     def bff_request(self, method: str, point: str, **kwargs) -> Any | Response:
#         """指挥中心"""
#         return self.currency_request(
#             _module="pro_bff",
#             method=method,
#             point=point,
#             resful=False,
#             **kwargs
#         )

#     # 4. center
#     def center_request(self, method: str, point: str, resful: bool = True, **kwargs) -> Any | Response:
#         """center"""
#         return self.currency_request(
#             _module="center",
#             method=method,
#             point=point,
#             resful=resful,
#             **kwargs
#         )
